﻿using BCCommon;
using BCData.TMS.Driver.DriverExamine;
using BCDto.TMS.Driver.DriverExamine;
using BCEntity.TMS.Driver.DriverExamine;
using BCService.TMS.Driver.DriverExamine;
using BCService.Utils;
using System;
using System.Collections.Generic;
namespace ATSimService.NewLogicService.AdminPanelService.DriverExamine
{
    public class DriverExaminePaperService : IDriverExaminePaperService
    {
        protected readonly IDriverExaminePaperData examinePaperData;
        protected readonly IRedisService redisService;
        protected readonly string entityName = "DriverExaminePaperEntity";

        public DriverExaminePaperService(IDriverExaminePaperData examinePapersData, IRedisService redisService)
        {
            this.examinePaperData = examinePapersData;
            this.redisService = redisService;
        }

        public DriverExaminePaperDto Add(DriverExaminePaperRequestDto dto)
        {
            var entity = dto.As<DriverExaminePaperEntity>();
            var result = examinePaperData.InsertAsync(entity).GetAwaiter().GetResult();
            if (result == null)
            {
                throw new ArgumentException("添加司机考试试卷失败。");

            }
            return result.As<DriverExaminePaperDto>();
        }

        public void Delete(int paperId)
        {
            bool result = examinePaperData.DeleteAsync(paperId).GetAwaiter().GetResult();
            if (result)
            {
                string key = string.Format("{0}@PaperId:{1}", entityName, paperId);
                redisService.Delete(key);
            }
        }

        public PageableList<DriverExaminePaperDto> GetDriverExaminePapers(int pageIndex, int pageSize)
        {
            var queryResult = examinePaperData.GetDriverExaminePapersAsync(pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<DriverExaminePaperDto>
            {
                Items = queryResult.Item1.As<IEnumerable<DriverExaminePaperDto>>(),
                Count = queryResult.Item2,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }

        public DriverExaminePaperDto GetExaminePaperById(int paperId)
        {
            string key = string.Format("{0}@PaperId:{1}", entityName, paperId);

            DriverExaminePaperEntity entity = this.redisService.TryGet(key, () => examinePaperData.GetExaminePaperByIdAsync(paperId)).GetAwaiter().GetResult();

            return entity.As<DriverExaminePaperDto>();
        }

        public DriverExaminePaperDto Update(long paperId, DriverExaminePaperRequestDto dto)
        {
            string key = string.Format("{0}@PaperId:{1}", entityName, paperId);

            var entity = dto.As<DriverExaminePaperEntity>();

            if (!examinePaperData.CheckExistsByIdAsync(entity.PaperId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("PaperIdNotExists");
            }
            var result = examinePaperData.UpdateAsync(entity).GetAwaiter().GetResult();

            if (result != null)
            {
                if (this.redisService.Exists(key))
                {
                    this.redisService.Delete(key);
                }
            }

            return result.As<DriverExaminePaperDto>();
        }
    }
}
